import request from '../utils/request';

/**
 * 创建设备
 * @param {string} typeUuid 设备类型Uuid
 * @param {string} factoryUuid 工厂Uuid
 * @param {string} name 设备名称
 * @param {string} serialNumber 序列号/SN码
 * @param {string} description 设备描述
 * @param {number} latitude 纬度
 * @param {number} longitude 经度
 * @param {string} location 设备地址
 */
export function createDeviceApi(
  typeUuid,
  factoryUuid,
  name,
  serialNumber,
  description,
  latitude,
  longitude,
  location
) {
  return request({
    url: '/devices',
    method: 'post',
    data: {
      type_uuid: typeUuid,
      factory_uuid: factoryUuid || null,
      name: name,
      serial_number: serialNumber,
      description: description || null,
      latitude: latitude || null,
      longitude: longitude || null,
      location: location || null
    }
  });
}

/**
 * 删除设备
 * @param {string} uuid
 */
export function deleteDeviceApi(uuid) {
  return request({
    url: '/devices/' + uuid,
    method: 'delete'
  });
}

export function deleteSelectedDeviceApi(uuidList) {
  return request({
    url: '/devices',
    method: 'delete',
    data: {
      uuid_list: uuidList
    }
  });
}

/**
 * 更新设备
 * @param {string} uuid path
 * @param {string} name 设备名称
 * @param {string} serialNumber 序列号
 * @param {string} description 设备描述
 * @param {number} latitude 纬度
 * @param {number} longitude 经度
 * @param {string} versionName 版本名称
 * @param {string} status 设备状态
 * @param {string} location 设备地址
 */
export function updateDeviceApi(
  uuid,
  typeUuid,
  factoryUuid,
  name,
  serialNumber,
  description,
  latitude,
  longitude,
  location,
  status
) {
  return request({
    url: '/devices/' + uuid,
    method: 'put',
    data: {
      type_uuid: typeUuid,
      factory_uuid: factoryUuid || null,
      name: name || null,
      serial_number: serialNumber,
      description: description || null,
      latitude: latitude,
      longitude: longitude,
      location: location,
      status: status || null
    }
  });
}

/**
 * 获取设备列表
 * @param {number} pageIndex 页码下标，默认从0开始
 * @param {number} pageSize 每页大小
 * @param {string} sortType 排序
 * @param {string} typeUuid 设备类型Uuid
 * @param {string} factoryUuid 工厂Uuid
 * @param {string} name 设备名称
 * @param {string} serialNumber 序列号
 * @param {string} versionName 版本名称
 * @param {string} status 设备状态
 * @param {boolean} active 是否可用
 */
export function getDeviceListApi(
  pageIndex,
  pageSize,
  sortType,
  orderBy,
  typeUuid,
  factoryUuid,
  name,
  serialNumber,
  status,
  active
) {
  return request({
    url: '/devices',
    method: 'get',
    params: Object.assign(
      {
        page_index: pageIndex,
        page_size: pageSize,
        sort_type: sortType,
        order_by: orderBy
      },
      typeUuid ? { type_uuid: typeUuid } : null,
      factoryUuid ? { factory_uuid: factoryUuid } : null,
      name ? { name } : null,
      serialNumber ? { serial_number: serialNumber } : null,
      status ? { status } : null,
      typeof active === 'boolean' ? { active: active } : null
    )
  });
}

/**
 * 获取设备信息接口
 * @param {string} uuid path
 */
export function getDeviceInfoApi(uuid) {
  return request({
    url: '/devices/' + uuid,
    method: 'get'
  });
}

/**
 * 查询设备接口
 * @param {string} name 设备名称
 * @param {number} take 获取数量 5-100
 */
export function queryDeviceListApi(name, take) {
  return request({
    url: '/devices/query',
    method: 'get',
    params: { name, take }
  });
}

/**
 * 获取设备历史数据
 */

export function getDeviceHistoryListApi(pageIndex, pageSize, deviceUuid, startTime, endTime) {
  return request({
    url: '/devices/histories',
    method: 'get',
    params: {
      page_index: pageIndex,
      page_size: pageSize,
      device_uuid: deviceUuid,
      start_time: startTime,
      end_time: endTime
    }
  });    
}

/**
 * 根据uuid和事件范围返回设备数据
 * @param {string} uuidList 
 * @param {number} startTime 
 * @param {number} endTime 
 */
export function getDeviceDataFromRangeApi(uuidList, startTime, endTime) {
  const baseUrl = (process.env.VUE_APP_BASE_API).split('/admin')[0];
  return request({
    url: baseUrl + '/dashboard/devices',
    method: 'post',
    data: {
      start_time: startTime,
      end_time: endTime,
      device_uuid_list: uuidList
    }
  });
}

/**
 * 获取设备状态下的设备数量列表
 */
export function getDeviceStatusCountListApi() {
  const baseUrl = (process.env.VUE_APP_BASE_API).split('/admin')[0];
  return request({
    url: baseUrl + '/dashboard/devices/status',
    method: 'get'
  });
}

/**
 * 获取首页汇总记录
 */
export function getDeviceSummaryApi() {
  const baseUrl = (process.env.VUE_APP_BASE_API).split('/admin')[0];
  return request({
    url: baseUrl + '/dashboard/devices/summary',
    method: 'get'
  });
}

/**
 * 安全风险曲线
 * @param {number} startTime 
 * @param {number} endTime 
 */
export function getDeviceTendencyApi(startTime, endTime) {
  const baseUrl = (process.env.VUE_APP_BASE_API).split('/admin')[0];
  return request({
    url: baseUrl + '/dashboard/alarm_histories/tendency',
    method: 'get',
    params: {
      start_time: startTime,
      end_time: endTime
    }
  });
}

/**
 * 修改设备连接密码
 * @param {string} uuid 
 * @param {string} password 
 */
export function updateDevicePasswordApi(uuid, password) {
  return request({
    url: '/devices/' + uuid + '/password',
    method: 'put',
    data: { password }
  });
}
